import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddOEQuestion extends JFrame implements ActionListener, FocusListener{
	
/*
(BorderLayout CENTER)
Row1:	Type the question here
Row2:	What type of response?
(BorderLayout SOUTH)
Row3:	Cancel and Submit Buttons
*/
	
	private JFrame mainFrame;
	private JPanel main, row1, row2, row3;
	private JTextField question;
	private JButton cancelButton, submitButton;
	private JLabel	responseType;
	private JComboBox comboBox;
	
	public AddOEQuestion(){
		super("Add an Open-Ended Question");

		mainFrame = new JFrame("Add an Open-Ended Question");
		main = new JPanel();
		main.setLayout(new BoxLayout(main,BoxLayout.Y_AXIS));
		row1 = new JPanel(new FlowLayout());
		row2 = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		
		question = new JTextField("Type your question here...");
		question.setPreferredSize(new Dimension(285,20));
		responseType = new JLabel("What type of response would you like?");
		comboBox = new JComboBox(new String[] {"Short Answer","Essay"});
		comboBox.setSelectedIndex(0);
		cancelButton = new JButton("Cancel");
		submitButton = new JButton("Submit");
		
		row1.add(question);
		row2.add(responseType);
		row2.add(comboBox);
		row3.add(cancelButton);
		row3.add(submitButton);
		main.add(row1);
		main.add(row2);
				
		// add action listeners to each of the buttons
		question.addFocusListener(this);
		cancelButton.addActionListener(this);
	    submitButton.addActionListener(this);

		mainFrame.add(main, BorderLayout.NORTH);
		mainFrame.add(row3, BorderLayout.SOUTH);
		mainFrame.setSize(500,500);
		mainFrame.setVisible(true);
	}

	public void focusGained(FocusEvent e){
		if(e.getSource()==question){
			if(question.getText().equals("Type your question here...")){
				question.selectAll();
			}
		}
	}
	
	public void focusLost(FocusEvent e){}
	
	public void actionPerformed( ActionEvent event){
		
		//Code for handling actions
		if(event.getSource()==cancelButton){
			
			//Go to the last page--probably the page where a survey preview can be seen
			
		}else if(event.getSource()==submitButton){
			if(question.getText().equals("") || question.getText().equals("Type your question here...")){
				JOptionPane.showMessageDialog(this, "Please enter a question.", "Warning!", JOptionPane.WARNING_MESSAGE);
			}
			
			//Database interaction--submit data to database
			
		}
	}

	public static void main(String args[]){
		AddOEQuestion application = new AddOEQuestion();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}